namespace Vanilla.UI

import Vanilla

import System
import System.Runtime.InteropServices

// PIXELS ARRAY - TODO: implement convenience stuff for accessing BitmapCanvas's raw pixels, perhaps in a GetPixels and BlitPixels manner or something
//class PixelArray of T (
//<mdx4ever> public static T[] ByteArrayToStructureArray<T>(byte[] byteArray) where T:struct
//<mdx4ever> {
//<mdx4ever> int itemSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(T));
//<mdx4ever> int count = byteArray.Length / itemSize;
//<mdx4ever> T[] result = new T[count];
//<mdx4ever> GCHandle handle= GCHandle.Alloc(result, GCHandleType.Pinned);
//<mdx4ever> Marshal.Copy(byteArray, 0, hand.AddrOfPinnedObject(), byteArray.Length);
//<mdx4ever> handle.Free()
//<mdx4ever> return result;
//<mdx4ever> }

// BITMAP CANVAS
partial class BitmapCanvas (Canvas):
	enum Format:
		ARGB32
		RGB24
		A8
		A1
	
	def constructor (f as Format, w as int, h as int):
		super(Cairo.ImageSurfaceCreate(f,w,h))
	
	def constructor (w as int, h as int):
		self(Format.ARGB32, w, h)
	
	Size as Size:
		get:
			return Size(Cairo.ImageSurfaceGetWidth(Handle), Cairo.ImageSurfaceGetHeight(Handle))
	
	RawData as IntPtr:
		get:
			return Cairo.ImageSurfaceGetData(Handle)
		
	// Create with existing in-memory bitmap
	def constructor (data as IntPtr, f as Format, w as int, h as int, stride as int):
		super(Cairo.ImageSurfaceCreateForData(data, f, w, h, stride))
	
	// Create from PNG file
	def constructor (filename as string):
		super(Cairo.ImageSurfaceCreateFromPNG(filename))
	
	// Load file
	static def Load (file as File) as BitmapCanvas:
		t = file.Type
		tool = t.ToolForAction("LoadBitmap")
		plugin = tool as ILoadBitmap
		return plugin.LoadBitmap(file)
	
	// Save file
	def Save (file as File):
		t = file.Type
		tool = t.ToolForAction("SaveBitmap")
		plugin = tool as ISaveBitmap
		plugin.SaveBitmap(file, self)
